package com.bpms.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bpms.InitServlet;
import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.Param;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.SV;
import com.bpms.report.CommonGenReport;

/**
 * 显示
 * 
 * @author Administrator
 * 
 */
public class ShowLSReport extends HttpServlet {
	public ShowLSReport() {
		super();
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		try {
			//loadParam();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			int page = 1;//获取的参数
			String d = format.format(new Date());
			
			if (request.getParameter("d") != null && !"".equals(request.getParameter("d").trim())){
				try {
				Date date = format.parse(request.getParameter("d"));
				d = format.format(date);
				} catch (Exception e) {
					CommonUtil.debug("ShowHDReport get Page Parameter exception", e);
				}
			}
			String sql = "select a.createdate||' '||a.createtime AS CREATDATE,a.payeeaccno,decode(a.trantype,'01','本行支票'，'02','他行支票','03','贷记凭证',a.trantype) as trantype,a.bizid,b.name from cm_djb a,accountinfo b"; 
			String where = " where 1=1 and a.createdate = '" + d +"' and (a.cardid=b.idcard or a.cardid= b.iccard) ";
			sql += where;
			String sql2 = "select a.trantype, count(a.trantype) as count from cm_djb a where 1=1 and a.createdate = '" + d +"' ";			
			
			
			String type = request.getParameter("t");
			if(type != null){
				if(type.equals("1")){
					sql += " and a.trantype='01'";
					sql2 += " and a.trantype='01'";
				}
				else if(type.equals("2")){
					sql += " and a.trantype='02'";
					sql2 += " and a.trantype='02'";
				}
				else if(type.equals("3")){
					sql += " and a.trantype='03'";
					sql2 += " and a.trantype='03'";
				}
			}
			
			sw =Param.getValue("BBDY", "SW");
			xw =Param.getValue("BBDY", "XW");
			
			String changci = request.getParameter("c");
			String cc = "";
			if(changci != null){
				if(changci.equals("1")){
					sql += " and  "+ sw;
					sql2 += " and "+ sw;
					cc = "上午";
				}
				else if(changci.equals("2")){
					sql += " and " + xw;
					sql2 += " and " + xw;
					cc = "下午";
				}else
					cc = "全天";
			}
			
			VOList vol = loadData(sql);//查询出的数据
			int total=0,djpz=0,bhzp=0,thzp=0,zp=0;
			VOList volsummary = loadData(sql2 + " group by trantype");
			Vector<VO> vector = volsummary.listData;
			for (VO v2 : vector) {
				String trantype = v2.getAttr("trantype");
				String count = v2.getAttr("count");
				if("01".equals(trantype)){
					try{
					bhzp = Integer.parseInt(count);
					}catch(Exception e){
						CommonUtil.debug(e);
					}
				}else if("02".equals(trantype)){
					try{
						thzp = Integer.parseInt(count);
						}catch(Exception e){
							CommonUtil.debug(e);
						}
				}else if("03".equals(trantype)){
					try{
						djpz = Integer.parseInt(count);
						}catch(Exception e){
							CommonUtil.debug(e);
						}
				}
			}
			
			total = djpz + bhzp + thzp;
			zp = bhzp + thzp;
			
			if((vol != null)&&(vol.rowCount >0)){
				byte[] buffer = genLSReport(Integer.toString(total),cc,Integer.toString(djpz),Integer.toString(zp),vol, InitServlet.RealPath, InitServlet.RealPath + "report/lsreport.jasper");
				response.setContentType("application/pdf");
				response.setCharacterEncoding("gb2312");
				response.setContentLength(buffer.length);
				ServletOutputStream ouputStream = response.getOutputStream();//写出数据
				ouputStream.write(buffer, 0, buffer.length);
				ouputStream.flush();
				ouputStream.close();
			}else{
				byte[] buffer = "无有效数据".getBytes("gb2312");
				response.setCharacterEncoding("gb2312");
				response.setContentLength(buffer.length);
				ServletOutputStream ouputStream = response.getOutputStream();//写出数据
				ouputStream.write(buffer, 0, buffer.length);
				ouputStream.flush();
				ouputStream.close();
			}
			
		} catch (Exception e) {
			CommonUtil.debug("Show ATM LOG exception", e);
		}

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

	/**
	 * 
	 * @param vol
	 *            //查询需要的数据 保存在vol中
	 * @param realpath
	 *            //存取根路径
	 * @param reportfile
	 *            //获取生成的报表路径
	 * @return
	 */
	private byte[] genLSReport(String total, String cc, String djpz, String zp, VOList vol, String realpath, String reportfile) {

		VO vd = new VO();
		vd.setObj("volist", vol);
		vd.setAttr("total", total);
		vd.setAttr("cc", cc);
		vd.setAttr("djpz", djpz);
		vd.setAttr("zp", zp);

		VO vr = SV.g("report.Lsprintreport", vd);
		CommonUtil.debug("vo return from report.hdprintreport is " + (VO) ((VOList) vr.getObj("list")).listData.get(0));

		VO p = new VO();
		p.setAttr("path", reportfile);
		CommonGenReport cgr = new CommonGenReport();
		byte[] buffer = cgr.genPdfReport(p, vr, realpath);
		CommonUtil.debug("genpdfreport pdf file size " + buffer.length);
		return buffer;
	}

	/**
	 * 加载数据
	 * 
	 * @param sql查询语句
	 * @return 返回值是list集合
	 */
	private VOList loadData(String sql) {
		try {
			JdbcWorker j = new JdbcWorker();
			VOList vol = j.readAll(new VO(), sql);
			
			CommonUtil.debug("volist is " + vol + " volist count:" + vol.rowCount);
			return vol;
		} catch (Exception e) {
			CommonUtil.debug("ShowLSReport exception", e);
		}
		return null;
	}

	String xw = "",sw="";
	private void loadParam(){
		try {
			String sql = "select * from bpm_paramvalue where paramid='BBDY'";
			JdbcWorker j = new JdbcWorker();
			VOList vol = j.readAll(new VO(), sql);
			CommonUtil.debug("volist is " + vol + " volist count:" + vol.rowCount);
			Vector<VO> vector = vol.listData;
			for (VO v2 : vector) {
				if("SW".equals(v2.getAttr("K"))){
					sw = v2.getAttr("VAL");
				}else if("SW".equals(v2.getAttr("K"))){
					xw = v2.getAttr("VAL");
				}
			}
		} catch (Exception e) {
			CommonUtil.debug("ShowLSReport exception", e);
		}
		
	}

	
}
